//-----------------------------------------------------------------------
//<copyright file="NhaCungCapDAO.cs
//Code generate by tools.
//</copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using PMS.Exceptions;
using System.Data.SqlClient;
using PMS.Entity;

namespace PMS.DAL
{
	public class NhaCungCapDAO : SQLDatabase, INhaCungCapDAO
	{

		#region Create NhaCungCap methods

		public NhaCungCap CreateNhaCungCap(NhaCungCap nhaCungCap)
		{
			try
			{

				SqlCommand command = SQLDatabase.StoredProcedureCommand("spNhaCungCapInsert");
				AddOutParameter(command, "@MaNCC", SqlDbType.NVarChar, nhaCungCap.MaNCC);
				AddInParameter(command, "@TenNCC", SqlDbType.NVarChar, nhaCungCap.TenNCC);
				AddInParameter(command, "@DiaChi", SqlDbType.NVarChar, nhaCungCap.DiaChi);
				AddInParameter(command, "@SoDT", SqlDbType.NVarChar, nhaCungCap.SoDT);
				AddInParameter(command, "@SoFax", SqlDbType.NVarChar, nhaCungCap.SoFax);
				AddInParameter(command, "@MaSoThue", SqlDbType.NVarChar, nhaCungCap.MaSoThue);
				AddInParameter(command, "@Email", SqlDbType.NVarChar, nhaCungCap.Email);
				AddInParameter(command, "@Website", SqlDbType.NVarChar, nhaCungCap.Website);
				AddInParameter(command, "@GhiChu", SqlDbType.NVarChar, nhaCungCap.GhiChu);

				ExecuteNonQuery(command);
                nhaCungCap.MaNCC = (string)GetParameterValue(command, "@MaNCC");
				return nhaCungCap;
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access CreateNhaCungCapException", ex);
			}
		}

		#endregion

		#region Update NhaCungCap methods

		public void UpdateNhaCungCap(NhaCungCap nhaCungCap)
		{
			try
			{
				SqlCommand command = SQLDatabase.StoredProcedureCommand("spNhaCungCapUpdate");

				AddInParameter(command, "@MaNCC", SqlDbType.NVarChar, nhaCungCap.MaNCC);
				AddInParameter(command, "@TenNCC", SqlDbType.NVarChar, nhaCungCap.TenNCC);
				AddInParameter(command, "@DiaChi", SqlDbType.NVarChar, nhaCungCap.DiaChi);
				AddInParameter(command, "@SoDT", SqlDbType.NVarChar, nhaCungCap.SoDT);
				AddInParameter(command, "@SoFax", SqlDbType.NVarChar, nhaCungCap.SoFax);
				AddInParameter(command, "@MaSoThue", SqlDbType.NVarChar, nhaCungCap.MaSoThue);
				AddInParameter(command, "@Email", SqlDbType.NVarChar, nhaCungCap.Email);
				AddInParameter(command, "@Website", SqlDbType.NVarChar, nhaCungCap.Website);
				AddInParameter(command, "@GhiChu", SqlDbType.NVarChar, nhaCungCap.GhiChu);

				ExecuteNonQuery(command);
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access UpdateNhaCungCapException", ex);
			}
		}

		#endregion

		#region Delete NhaCungCap methods

		public void DeleteNhaCungCap(NhaCungCap nhaCungCap)
		{
			try
			{
				SqlCommand command = SQLDatabase.StoredProcedureCommand("spNhaCungCapDelete");

				AddInParameter(command, "@MaNCC", SqlDbType.NVarChar, nhaCungCap.MaNCC);

				ExecuteNonQuery(command);
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access DeleteNhaCungCapException", ex);
			}
		}

		#endregion

		#region Select methods

		#region GetAll NhaCungCap methods

		public NhaCungCaps GetAllNhaCungCap()
		{
			try
			{

				SqlCommand command = SQLDatabase.StoredProcedureCommand("spNhaCungCapGetList");
				NhaCungCaps Result = new NhaCungCaps();
				List<NhaCungCap> List = ExecuteQuery<NhaCungCap>(command,true);
				foreach(NhaCungCap item in List)
				{
					Result.Add(item);
				}
				return Result;
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access GetAllNhaCungCapException", ex);
			}
		}

		#endregion


		#region GetNhaCungCap methods

		public NhaCungCap GetNhaCungCapByID(string maNCC)
		{
			try
			{
				SqlCommand command = SQLDatabase.StoredProcedureCommand("spNhaCungCapGet");

				AddInParameter(command, "@maNCC", SqlDbType.NVarChar, maNCC);

				return ExecuteQuery<NhaCungCap>(command);
			}
			catch (Exception ex)
			{
				throw DataAccessException.GetException("Data Access GetNhaCungCapBy ID Exception", ex);
			}
		}

		#endregion


		#endregion
	}
}//End Class
